package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.pojo.DetailTabPojo;
import com.xbongbong.paas.service.DetailTabConfigService;
import com.xbongbong.pro.detailtab.pojo.TabConfigPojo;
import com.xbongbong.pro.domain.entity.DetailTabConfigEntity;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.DetailTabEnum;
import com.xbongbong.saas.model.DetailTabConfigModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/02/11 16:53
 */
@Service("detailTabConfigServiceImpl")
public class DetailTabConfigServiceImpl implements DetailTabConfigService {
    @Resource
    private DetailTabConfigModel detailTabConfigModel;

    @Override
    public List<DetailTabPojo> handleDetailTabSort(String userId, String corpid, Long formId, Integer businessType, List<DetailTabPojo> tabPojoList) {
        DetailTabConfigEntity detailConfig = detailTabConfigModel.getEntityByBusinessTypeAndFormId(corpid, userId, businessType, formId);
        if (Objects.nonNull(detailConfig)) {
            List<TabConfigPojo> tabConfigPojos = JSONArray.parseArray(detailConfig.getTabConfig(), TabConfigPojo.class);
            Map<String, TabConfigPojo> configMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            tabConfigPojos.forEach(tabConfigPojo -> configMap.put(tabConfigPojo.getAttr(), tabConfigPojo));
            List<DetailTabPojo> detailTabPojos = new ArrayList<>();
            TabConfigPojo tabConfigPojo;
            for (DetailTabPojo detailTabPojo : tabPojoList) {
                tabConfigPojo = configMap.get(detailTabPojo.getAttr());
                if (Objects.nonNull(tabConfigPojo)) {
                    detailTabPojo.setSort(tabConfigPojo.getSort());
                    detailTabPojo.setEnable(tabConfigPojo.getEnable());
                    detailTabPojos.add(detailTabPojo);
                }
            }
            detailTabPojos.removeIf(tabPojo -> !Objects.equals(tabPojo.getEnable(), BasicConstant.IS_USE));
            detailTabPojos.sort((pojo1, pojo2) -> pojo1.getSort() - pojo2.getSort());
            for (DetailTabPojo detailTabPojo : tabPojoList) {
                if (!configMap.containsKey(detailTabPojo.getAttr())) {
                    detailTabPojos.add(detailTabPojo);
                }
            }
            tabPojoList = detailTabPojos;
        }else{
            //审批相关tab，默认放最后面（产品说的）
            //过滤下是否有审批tab
            boolean hasApproval = false;
            List<DetailTabPojo> pojoList = new ArrayList<>();
            for (DetailTabPojo detailTabPojo : tabPojoList) {
                if(Objects.equals(detailTabPojo.getAttr(), DetailTabEnum.APPROVAL_RECORDS.getKey())){
                    hasApproval = true;
                    pojoList.add(detailTabPojo);
                }
                if(Objects.equals(detailTabPojo.getAttr(), DetailTabEnum.APPROVAL_PROCESS.getKey()) || Objects.equals(detailTabPojo.getAttr(), DetailTabEnum.APPROVAL_HISTORY.getKey())){
                    hasApproval = true;
                    pojoList.add(detailTabPojo);
                }
            }
            if(hasApproval){
                tabPojoList.removeIf(pojoList :: contains);
                tabPojoList.addAll(pojoList);
            }
        }
        return tabPojoList;
    }
}
